package com.example.zhangwei.util;

import com.example.zhangwei.ZhangweiTextApplication;
import com.example.zhangwei.widgts.dialog.androidTVBuilder;

import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnFocusChangeListener;
import android.view.ViewGroup.LayoutParams;

public class BaseActivity extends Activity{
	
	/**
     * 屏幕分辨率宽度
     */
    public int mFillwindowWidth;

    /**
     * 屏幕分辨率高度
     */
    public int mFillwindowHeight;

    /**
     * 屏幕密度
     */
    public float mDensity;
    
    public ZhangweiTextApplication mApplication;

	@SuppressWarnings("deprecation")
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		mApplication = (ZhangweiTextApplication) BaseActivity.this.getApplication();
		mDensity = getResources().getDisplayMetrics().density;
		mFillwindowWidth = getWindowManager().getDefaultDisplay().getWidth();
		mFillwindowHeight = getWindowManager().getDefaultDisplay().getHeight();
	}
	
	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
	}
	
	@Override
	protected void onStop() {
		// TODO Auto-generated method stub
		super.onStop();
	}
	
	
	public void changeWindow(Activity c1, Class<?> c2, Bundle bd) {
        Intent intent = new Intent();
        if (bd != null) {
        	intent.putExtras(bd);
        }
        intent.setClass(c1, c2);
        c1.startActivity(intent);
    }
	
	public void changeWindowIntent(Activity c1, Class<?> c2, Intent intent) {
        intent.setClass(c1, c2);
        c1.startActivity(intent);
    }

    public void changeTo(Class<?> subView) {
        changeWindow(this, subView, null);
    }

    public void changeTo(Class<?> subView, Bundle bd) {
        changeWindow(this, subView, bd);
    }
    
    public void changeTo(Class<?> subView, Intent intent) {
    	changeWindowIntent(this, subView, intent);
    }

    public static void changeTo(Context context, Class<?> subView) {
        Intent intent = new Intent(context, subView);
        context.startActivity(intent);
    }

    public static void changeTo(Context context, Class<?> subView, Bundle bd) {
        Intent intent = new Intent(context, subView);
        intent.putExtras(bd);
        context.startActivity(intent);
    }
    
    /**
     * 设置可以点击事件
     * 
     * @param activity
     * @param obj 控件
     */
    protected void setOnClick(Activity activity, View obj)
    {
        obj.setOnClickListener((OnClickListener) activity);
    }

    /**
     * 设置可以焦点事件
     * 
     * @param activity 
     * @param obj
     */
    protected void setOnFocus(Activity activity, View obj)
    {
        obj.setFocusable(true);
        obj.setFocusableInTouchMode(true);
        obj.setOnFocusChangeListener((OnFocusChangeListener) activity);
    }

    protected void setClickFocus(Activity activity, View obj)
    {
        setOnClick(activity, obj);
        setOnFocus(activity, obj);
    }

    /**
     * 设置控件的宽、高
     * 
     * @param v {@link View}控件
     * @param width 宽
     * @param height 高
     */
    protected void setMarginRect(View v, int width, int height)
    {
        LayoutParams laParams = (LayoutParams) v.getLayoutParams();
        laParams.height = height;
        laParams.width = width;
        v.setLayoutParams(laParams);
    }
    
    /**
     * 产生定制对话框
     * 
     * @param activity 显示对方框的{@link Activity}
     * @param title 对话框标题
     * @param message 对话框显示的内容
     * @param Positive 确定按钮显示的文字
     * @param Negative 可选,取消按钮显示的文字
     * @param typeId 对话框类型ID
     */
    protected void showDialogMessage(Activity activity, String title, String message, String Positive, String Negative, int typeId)
    {
    	final int id = typeId;
        androidTVBuilder idb = new androidTVBuilder(activity);
        idb.setTitle(title);
        idb.setMessage(message);
        idb.setPositiveButton(Positive, new DialogInterface.OnClickListener()
        {
            @Override
            public void onClick(DialogInterface dialog, int which)
            {
                doPositive(id);
            }
        });
        if (!TextUtils.isEmpty(Negative))
        {
            idb.setNegativeButton(Negative, new DialogInterface.OnClickListener()
            {
                @Override
                public void onClick(DialogInterface dialog, int which)
                {

                }
            });
        }
        idb.show();
    }
    
    /**
     * 对话框中点确定执行的方法
     */
    protected void doPositive(int id)
    {
    }
   
	
}
